// pages/index/withdraw/withdraw.js
let network = require('../../../utils/requestManager.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1,
    tabArray: [{
      name: "充值记录"
    }, {
      name: "提现记录"
    }],
    win_height: '',
    record_list:[],
    cz_next_url:'backend/shop/v1/shop/api/seller/shop_info/charge_money/obtain',
    tx_next_url: 'backend/shop/v1/shop/api/seller/shop_info/withdraw_money/obtain',
    no_data:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      win_height: app.globalData.winHeight
    })
    that.swichNav()
  },

  /**
   * 滚动到顶部
   * **/
  upper(e) {
    console.log(e)
  },

  /**
   * 滚动到底部
   * **/
  lower(e) {
    let that = this;
    if (that.data.no_data){
      that.swichNav()
      console.log("有数据")
    }else{
      console.log("无数据")
      return
    }
  },

  /**
   * 滚动时触发
   * **/
  scroll(e) {

  },

  /**
   * 判断类型
   * 如果是点击，清空数组
   * 根据类型去请求接口
   * 默认为0
   * **/
  swichNav: function (e) {
    let that = this;
    if(e){
      that.setData({
        record_list: [],
        no_data:false
      })
    }
    console.log("判断数据")
    if(that.data.no_data){
      return
    }
    let now_current = e ? e.target.dataset.current : that.data.currentTab;
    that.setData({
      currentTab: now_current
    })
    switch(now_current){
      case 0:
        that.requset_cz()
        break;
      case 1:
        that.request_withdraw()
        break;
    }
  },

  //充值记录请求
  requset_cz:function(){
    let that = this;
    console.log("充值接口")
    console.log(that.data.cz_next_url)
    network.getRequest(that.data.cz_next_url,{},function(res){
      console.log(res.detail.next)
      if(!res.detail.next){
        that.setData({
          no_data:true
        })
      }
      that.setData({
        record_list: that.data.record_list.concat.apply(that.data.record_list, res.detail.results),
        cz_next_url: res.detail.next
      })
    },function(err){
      console.log(err)
    })
  },

  //提现记录请求
  request_withdraw:function(){
    let that = this;
    console.log("提现接口")
    network.getRequest(that.data.tx_next_url, {}, function (res) {
      console.log(res)
      if (!res.detail.next) {
        that.setData({
          no_data: true
        })
      }
      that.setData({
        record_list: that.data.record_list.concat.apply(that.data.record_list, res.detail.results),
        tx_next_url: res.detail.next
      })
    }, function (err) {
      console.log(err)
    })
  },

  /**
   * 记录详情
   * **/
  to_wethdraw_detail: function (e) {
    console.log(e)
    let record_detail = JSON.stringify(e.currentTarget.dataset.detail);
    let record_type = e.currentTarget.dataset.type;
    switch(record_type){
      case 'charge':
        wx.navigateTo({
          url: '../recharge/recharge_detail?detail=' + record_detail,
        })
        break;

      case 'withdraw':
        wx.navigateTo({
          url: '../withdraw_record/withdraw_detail?detail=' + record_detail,
        })
        break;

        default:
        wx.navigateTo({
          url: '../recharge/recharge_detail?id=' + e.currentTarget.dataset.id,
        })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})